var MochaWindowHandler =  new Class({

        Implements: [Events, Options],

        options: {
                defaultWindow: {
                        id: 'default_window',
                title: 'Default Window',
                width: 800,
                height: 600,
                contentBgColor: 'FFFFFF',
                        headerStartColor: [85, 118, 172],
                        headerStopColor: [85, 118, 172],
                        bodyBgColor: [256, 256, 256],
                        resizableColor: [85, 118, 172]
                },
                messageWindow: {
                        id: 'message_window',
                title: 'Message Window',
                width: 800,
                height: 600,
                contentBgColor: 'FFFFFF',
                        headerStartColor: [85, 118, 172],
                        headerStopColor: [85, 118, 172],
                        bodyBgColor: [256, 256, 256],
                        resizableColor: [85, 118, 172]
                },
                notificationWindow: {
                        id: 'notification_window',
                title: 'Notification Window',
                width: 500,
                height: 90,
                x:10,
                y:10,
                contentBgColor: 'FFFFFF',
                        bodyBgColor: [256, 256, 256]
                },
                errorWindow: {
                        id: 'error_window',
                title: 'Error Window',
                width: 800,
                height: 600,
                contentBgColor: 'FFFFFF',
                        headerStartColor: [220, 20, 60],
                        headerStopColor: [220, 20, 60],
                        bodyBgColor: [256, 256, 256],
                        resizableColor: [85, 118, 172]
                }
        },

        // constructor
        initialize: function(options){
                this.setOptions(options);

                window.addEvent('domready', function(){
                        this.domReady();
                }.bind(this));
        },

        domReady: function(){

                MochaUI.Modal = new MochaUI.Modal();

                // click handlers for auto element handling

                $$(".openMochaWindow").each(function(item,index){
                        var anchor = item;
                        anchor.addEvent('click',function(event){
                                new Event(event).stop();
                                this.openWindowFromElement(anchor,event);
                        }.bind(this));
                }.bind(this));

                $$(".openCustomMochaModalWindow").each(function(item,index){
                        var anchor = item;
                        anchor.addEvent('click',function(event){
                                new Event(event).stop();
                                this.openCustomModalWindowFromElement(anchor,event);
                        }.bind(this));
                }.bind(this));

        },

        // PUBLIC METHODS
------------------------------------------------------------------

        showNotification: function(title, message, timeLimit){
                var windowData = new Object();
                $extend(windowData, this.options.notificationWindow);

                windowData.type = 'notification';
                if(timeLimit){
                        windowData.closeAfter = (timeLimit*1000);
                }
                windowData.title = title;

                /*
                        <div class="mocha_notification">
                                <p>Message</p>
                        </div>
                */

                var messageHolder = new Element('div', {
                'class': 'mocha_notification'
                });

                var messageText = new Element('p');
                messageText.set('text',message);

                messageHolder.adopt(messageText);

                windowData.content = messageHolder;
                this.openWindow(windowData);
        },

        showMessage: function(title, message, width, height){
                var windowData = new Object();
                $extend(windowData, this.options.messageWindow);

                windowData.title = title;

                /*
                        <div class="mocha_message">
                                <p>Message</p>
                        </div>
                */

                var messageHolder = new Element('div', {
                'class': 'mocha_message'
                });

                var messageText = new Element('p');
                messageText.set('text',message);

                messageHolder.adopt(messageText);

                windowData.content = messageHolder;
                windowData.width = width;
                windowData.height = height;
                this.openWindow(windowData);
        },

        showErrorMessage: function(title, message, width, height){
                var windowData = new Object();
                $extend(windowData, this.options.errorWindow);

                windowData.title = title;

                /*
                        <div class="mocha_error_message">
                                <p>Message</p>
                        </div>
                */

                var messageHolder = new Element('div', {
                'class': 'mocha_error_message'
                });

                var messageText = new Element('p');
                messageText.set('text',message);

                messageHolder.adopt(messageText);

                windowData.content = messageHolder;
                windowData.width = width;
                windowData.height = height;
                this.openWindow(windowData);
        },

        showMessageConfirmation: function(title, message, label, width,
height, callback){
                var windowData = new Object();
                $extend(windowData, this.options.messageWindow);

                windowData.title = title;
                windowData.closable = false;
                windowData.resizable = false;

                /*
                        <div class="mocha_message">
                                <p>Message</p>
                                <div class="confirmButton"/>Label</div>
                        </div>
                */

                var messageHolder = new Element('div', {
                'class': 'mocha_message'
                });

                var messageText = new Element('p');
                messageText.set('text',message);

                var messageOK = new Element('div', {
                    'id': 'messageButton',
                    'text': label
                });

                messageOK.addEvent('click',function(e){
                        //MochaUI.closeAll();
                        MochaUI.closeWindow($('message_window'));
                        eval(callback)(e);
                });

                messageHolder.adopt(messageText);
                messageHolder.adopt(messageOK);

                windowData.content = messageHolder;
                windowData.width = width;
                windowData.height = height;
                this.openWindow(windowData);
        },

        showErrorConfirmation: function(title, message, label, width, height,
callback){
                var windowData = new Object();
                $extend(windowData, this.options.errorWindow);

                windowData.title = title;
                windowData.closable = false;
                windowData.resizable = false;

                /*
                        <div class="mocha_error_message">
                                <p>Message</p>
                                <div class="confirmButton"/>Label</div>
                        </div>
                */

                var messageHolder = new Element('div', {
                'class': 'mocha_error_message'
                });

                var messageText = new Element('p');
                messageText.set('text',message);

                var messageOK = new Element('div', {
                    'class': 'confirmButton',
                    'text': label
                });

                messageOK.addEvent('click',function(e){
                        //MochaUI.closeAll();
                        MochaUI.closeWindow($('error_window'));
                        eval(callback)(e);
                });

                messageHolder.adopt(messageText);
                messageHolder.adopt(messageOK);

                windowData.content = messageHolder;
                windowData.width = width;
                windowData.height = height;
                this.openWindow(windowData);
        },

        showMessageOptions: function(title, message, posLabel, negLabel,
width, height, callback){
                var windowData = new Object();
                $extend(windowData, this.options.messageWindow);

                windowData.title = title;
                windowData.closable = false;
                windowData.resizable = false;

                /*
                        <div class="mocha_message">
                                <p>message</p>
                                <div class="option1Button"/>posLabel</div>
                                <div class="option2Button"/>negLabel</div>
                        </div>
                */

                var messageHolder = new Element('div', {
                'class': 'mocha_message'
                });

                var messageText = new Element('p');
                messageText.set('text',message);

                var messageYES = new Element('div', {
                    'class': 'option1Button',
                    'text': posLabel
                });

                messageYES.addEvent('click',function(e){
                        //MochaUI.closeAll();
                        MochaUI.closeWindow($('message_window'));
                        eval(callback)(true,e);
                });

                var messageNO = new Element('div', {
                    'class': 'option2Button',
                    'text': negLabel
                });

                messageNO.addEvent('click',function(e){
                        //MochaUI.closeAll();
                        MochaUI.closeWindow($('message_window'));
                        eval(callback)(false,e);
                });

                messageHolder.adopt(messageText);
                messageHolder.adopt(messageYES);
                messageHolder.adopt(messageNO);

                windowData.content = messageHolder;
                windowData.width = width;
                windowData.height = height;
                this.openWindow(windowData);
        },

        // PRIVATE METHODS
------------------------------------------------------------------

        openWindowFromElement: function(element,event){
                var windowData = new Object();
                $extend(windowData, this.options.defaultWindow);

                windowData = this.setWindowDataFromElement(element, windowData);
                windowData.loadMethod = 'iframe';
                this.openWindow(windowData);
        },

        setWindowDataFromElement: function(element, data){
                var title;
                var url;

                data.onClose = this.windowCloseHandler;

                if(title = element.get('title')){
                        data.title = title;
                }

                if(url = element.get('href')){
                        data.contentURL = url;
                }

                if(rel = element.get('rel')){
                        var options = rel.split('|');
                        if(options.length){
                                // eg 'modal|500|600'
                                // types 'window', 'modal', 'modal2', or 'notification'

                                data.type = options[0];
                                if(options[1]){
                                        data.width = options[1];
                                }
                                if(options[2]){
                                        data.height = options[2];
                                }
                        }
                }

                // scrolling y postion
                var pos = element.getPosition();
                data.y = pos.y;

                return data;
        },

        openWindow: function(windowData){
                new MochaUI.Window(windowData);
        },

        windowCloseHandler: function(){

        }

});

var mochaWindowHandler = new MochaWindowHandler();